package com.price.sample.ui

import android.app.DownloadManager
import android.content.Context
import android.net.Uri
import android.os.Bundle
import android.os.Environment
import android.view.View
import android.widget.EditText
import android.widget.TextView
import androidx.core.widget.addTextChangedListener
import com.price.common.base.BaseActivity
import com.price.common.extend.appLanguage
import com.price.common.extend.attachLanguage
import com.price.common.extend.download
import com.price.common.extend.logError
import com.price.common.extend.toast
import com.price.common.util.SpUtils
import com.price.mmkv.SpMMKVUtils
import com.price.sample.bean.TestBean
import com.price.util.MMKVRepository
import com.zp.common.R
import java.util.Locale


class OtherActivity : BaseActivity() {

    private lateinit var tvShow: TextView
    private lateinit var tvDown: TextView
    var downId:Long? = null

    override fun getViewId(): Int =R.layout.activity_other

    override fun initView(savedInstanceState: Bundle?) {

        tvShow = findViewById(R.id.tv_show)
        val ed = findViewById<EditText>(R.id.ed_content)
        ed.addTextChangedListener { edit ->
            var u = MMKVRepository.user
            if (u == null) {
                u = TestBean()
            }
            u.name = edit.toString()
            MMKVRepository.user = u
            showAction()

        }
        findViewById<View>(R.id.tv_save).setOnClickListener {
            val content = ed.text.toString()
            SpUtils.put("c", content)

            SpMMKVUtils.encode("text", "sp" + content)
            tvShow.text = SpMMKVUtils.decodeString("text")

        }
        showAction()


        findViewById<View>(R.id.tv_lau).setOnClickListener {
            appLanguage = Locale.ENGLISH
            recreate()
        }

        tvDown = findViewById<TextView>(R.id.tv_down)
        tvDown.setOnClickListener {
            if(downId!=null){
                toast("in downing")
                return@setOnClickListener
            }

            val downManager =
                download("https://download.apkpure.com/custom/com.apkpure.aegon-3201627.apk?_fn=QVBLUHVyZV92My4yMC4xNl9hcGtwdXJlLmNvbS5hcGs&as=ec4a61ce7b6bf41cdacd3ebdc148c04c66babe15&ai=1888620002&at=1723514269&_sa=ai%2Cat&k=095525c3e89fd312f4b96332f866adb066bd609d&r=https%3A%2F%2Fapkpure.com%2Ftw%2Fapkpure%2Fcom.apkpure.aegon&_p=Y29tLmFwa3B1cmUuYWVnb24&c=1%7CTOOLS%7CZGV2PUFQS1B1cmUmdD1hcGsmcz0xNTk1MDkxMCZ2bj0zLjIwLjE2JnZjPTMyMDE2Mjc&hot=1&arg=apkpure%3A%2F%2Fcampaign%2F%3Freport_context%3D%7B%22channel_id%22%3A%221001%22%7D") {
                    title = "文件下载"
                    notificationVisibility =
                        DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED
                    destinationUri = Uri.parse("")
                    mimeType = "application/vnd.android.package-archive"
                    setFinishOnTouchOutside(false)
                    destinationInExternalFilesDir(Environment.DIRECTORY_DOWNLOADS, "test.apk")
                    onStart { id ->
                        downId = id
                        SpUtils.put("before_id",id)
                        tvDown.text = "STATUS_START"
                    }
                    onChange { downloadedSize, totalSize, status ->
                        when (status) {
                            DownloadManager.STATUS_PENDING -> {
                                logError("STATUS_PENDING")
                                tvDown.text = "STATUS_PENDING"
                            }

                            DownloadManager.STATUS_RUNNING -> {
                                logError("STATUS_RUNNING")
                                var progress = (downloadedSize * 100.0 / totalSize).toInt()
                                if (progress < 0) progress = 0
                                tvDown.text = "STATUS_RUNNING:$progress%"
                            }
                            DownloadManager.STATUS_PAUSED -> {
                                logError("STATUS_PAUSED")
                                tvDown.text = "STATUS_PAUSED"
                            }
                            DownloadManager.STATUS_SUCCESSFUL -> {
                                logError("STATUS_SUCCESSFUL")
                                tvDown.text = "STATUS_SUCCESSFUL"
                            }
                            DownloadManager.STATUS_FAILED -> {
                                logError("STATUS_FAILED")
                                tvDown.text = "STATUS_FAILED"
                            }
                        }


                        logError("onChange:$downloadedSize  total:$totalSize status:$status")
                    }
                    onComplete { uri ->
                        downId=null
                        logError("onComplete:$uri")
                    }
                }

            //删除下载
            val id=SpUtils.get("before_id",0L)
            if(downId!=id){
                downManager?.remove(id!!)
            }

            logError("downId:$downId")
        }
    }

    override fun initData(savedInstanceState: Bundle?) {

    }

    override fun resume() {

    }

    private fun showAction() {
        val show = SpUtils.get<String>("c")

        val u = MMKVRepository.user
        tvShow.text = u?.name ?: "none data"
    }

    override fun attachBaseContext(newBase: Context?) {
        super.attachBaseContext(newBase?.attachLanguage())
    }
}